package cn.hfatec.shms.web.yk.ykXmmx.action;

import cn.hfatec.shms.web.yk.ykHsb.model.YkHsb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.yk.ykXmmx.model.YkXmmxParams;
import cn.hfatec.shms.web.yk.ykXmmx.model.YkXmmx;
import cn.hfatec.shms.web.yk.ykXmmx.service.YkXmmxService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/yk/ykXmmx")
public class YkXmmxAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "YkXmmxService")
    private YkXmmxService ykXmmxService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(YkXmmxParams ykXmmxParams, Page page, Model model) {

        Table table = ykXmmxService.queryYkXmmxTable(ykXmmxParams, page);
        model.addAttribute("ykXmmxsTable", table);
        model.addAttribute("ykXmmxParams", ykXmmxParams);
        return "/yk/ykXmmx/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(YkXmmxParams ykXmmxParams, Page page, Model model) {
        try {
            list(ykXmmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykXmmx/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            YkXmmx ykXmmx = new YkXmmx();
            model.addAttribute("ykXmmx", ykXmmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykXmmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            YkXmmx ykXmmx = ykXmmxService.getYkXmmx(id);
            model.addAttribute("ykXmmx", ykXmmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykXmmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteYkXmmx(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ykXmmxService.deleteYkXmmxBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveYkXmmx(@Valid YkXmmx ykXmmx, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ykXmmxService.saveYkXmmx(ykXmmx);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据YkXmmx保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }
    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goCheck")
    @ResponseBody
    public String goCheck(String hsTime,String hsEnd) {
        try {
            ykXmmxService.deleteYkXmmxhs(hsTime,hsEnd);
            List<YkXmmx> ykjg = ykXmmxService.getYkXmmxHs(hsTime,hsEnd);
            for(YkXmmx YkXmmxhs : ykjg) {
                ykXmmxService.saveYkXmmx(YkXmmxhs);
            }

            if (ykjg != null) {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", ykjg).toString();
            } else {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", new YkXmmx()).toString();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
